package pybaldaj.thrift;
import java.nio.ByteBuffer;
import java.util.Date;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;

import pybaldaj.thrift.generated.ThriftDemo;
import pybaldaj.thrift.generated.TimeStamp;

public class MainServer {

    public static class Thrift implements ThriftDemo.Iface {

        static int sequenceNumber;
        static int seqIncrement = 1;

        public void reset() throws TException {
            sequenceNumber = 0;
        }

        public void setIncrement(int increment) throws TException {
            seqIncrement = increment;
        }

        public TimeStamp get() throws TException {
            TimeStamp ts = new TimeStamp(sequenceNumber, ByteBuffer.wrap((new Date()).toString().getBytes()));
            sequenceNumber++;
            return ts;
        }

    }

    protected static void doMain(final String[] args) throws Exception {
        Thrift thrift = new Thrift();
        ThriftDemo.Processor processor = new ThriftDemo.Processor(thrift);
        TServerTransport serverTransport = new TServerSocket(18999);
        TProtocolFactory protFactory = new TBinaryProtocol.Factory(true, true);
        TServer server = new TThreadPoolServer(processor, serverTransport,
                protFactory);
        server.serve();
    }

    public static void main(String[] args) throws Exception {
        doMain(args);
    }
}
